package com.yuyang.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.yuyang.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000122639188";

    // 应用私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCMSkCf/tem7HWR2QVZA1rpOs1Hgnet/SJSv6KlZdIDZYlpNHZh1N4mqtq7K25kzuDGotMzoM65ahBMKgnZbuHl/m2SRc3q3lhI/Eftco3EcvkFSOYv9n4xWhOpxgc+M77qocGoc9M4xX1AoWSlIVwDeHpd5Iyw+Z4ZjDDmmssNQnE1m1bNchtT8nPtgRvECrvAW52DYSjsdu5kCD25tcjXE5KJqFKsrpx5+2I5qcFC9rMzgkqCp1AcxGaTklGvmk3l1V2Dg9eAF0uYcNOFDmH1AbUDnAerbXBEIOphR/d43DKs+YPVj/wRdqsbZqTHHq0vig7878LksvkiL2vliZIDAgMBAAECggEAdpT880CSQIJ3DoUGxCrV2XbetadOSj9Bl/hsHqpmToCxGcBCoIcyEDUzFiqr8YH89WoEZAt1Mby5cefG04eqYqIhNBbU68ZtkaPNmKPPuVIFs7Xe/IO6PzUIkeTVknpi0ilQbHca/03PhaXmUL5V7IkVxe++DImyXZ3mZ8ZIBDf4oEXMtcWcmCjj6FJS4LQLVicu8jBRw99s9yVINzRwB+/xnnpweupsWxl9goA4aYkL1xxcmwPyBL5zmTPag4+zfW71750KoLysuF3w9YPLJOC+z++SW26bEheLTj1oUSb+rKX+Ezn7x/aYqSzYp0xnwUAcCnZ83xjyDBaOVFdswQKBgQDFLrfuaD4AsahY+B5mfuVLmlRImS2xetuTLV+4ZKI2dg8fU81v7oEPmPNky+mep+xXbWy5od0+9OGs1lWEHveBrnaYnxc4dnArBOc+6pRypYKOqr++HHFLro+7yXa7M2IrsutiTPHXAJ/aYYC/NTbH5OyQOQ+edzJZuJ/m1lh+uQKBgQC2IxqUzGndwR2hP0Kaam+GsLEvpH9GLhy/gHC8XDVr6ObBe8iqCvhVyVMlw2JKmWgzcEaIxBkRSDIGZgJHz0DQCZb8NM1coYxMdIHUQAZ5US3oxSdTghPwrMW1zZ/GqE0XPsxUeRJPRtdw4czlfs5MVpNgu8R0Z8g6iNDtZO+YmwKBgQCcZjgB47D6P+/10afbSF97HggoJUd29KnhDSZVTYzUgcPw5zmVxKmdw4Wat5qv+yWdGvAYNWkVnIq72q2txBoxRD5RRC7Y3CiQFq2M2+evH9R0cL7xfgHnM6hSHLQOO+1sF9hX+gwAq0AfqSLDhuPSnaN9qaTX3leTjrtWD0HWEQKBgDcyWjrf2/F1NdqJBlEvkk7aO0g57MxC6zlp3kBnHVVvhob6UWye2HV/BrC6Do6XAQodq3KFIyemnmpr/aLvfy/MyDH0eovo9VCyOiotoQig/m29Wdan5A0a/uvCvfoBRSOXR2sOKetWh7HQ7Dxcen6N+inlnXq9fOOoVNCstyOjAoGAE4midmgG5U8o7PGmpGQuqwkS7O6Q6V0VeD+R8XxhtjdsCwv73qSV83aZFhlXg3n3Shp2/pCxu0AxUUnuIaprUr7hy3co+nPU1qE6cMrLaJ5YFlzM4Buea6TTomss8QVgf3aOLeTN1HMCKcgm3MDzDCitMQVKffHWxMlKt6ewLFw=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuIbTWvROSGh9sBqbufZKV5LhHmFI0hKl7bllLk5UhTVMfbqPxp6mAPiXGktn6qBr5TkMA4hZSeMA2D1ES0u4LylZtifL1Ku8XvNcTW//NvBNlSfZxAbYHuxSpPCITxe+Tx5VQsqDBVhZZ85KIZocBADfiNKwSIBdoOgx35wnEFCbyXQiogTe8L/PAJ3BZ0EZPImaxO3ls8B8c6xlH+Agt7lOz4/wJBUaM44XGrgHUMsalSx9vbBUBCK4HDtrVH42/D9LNzhzjcQqAZLtx2N+MZoSMC9GnRpaiM7IQ7p668gUzwMBYjzeQAbegJaQasKqiPOE4aglOI/MXLyRXXrvgwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://tj3r7y.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
